package org.wmq.skyserver.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.wmq.skycommon.pojo.VO.TurnoverReportVO;
import org.wmq.skyserver.mapper.OrderMapper;
import org.wmq.skyserver.mapper.ReportMapper;
import org.wmq.skyserver.service.ReportService;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.*;

@Service
@RequiredArgsConstructor
public class ReportServiceImpl implements ReportService {
    private final OrderMapper orderMapper;

    @Override
    public TurnoverReportVO getTurnoverStatistics(LocalDate begin, LocalDate end) {
        StringJoiner dateJoiner = new StringJoiner(",");
        StringJoiner turnoverJoiner = new StringJoiner(",");
        for (LocalDate now = begin; !now.isAfter(end); now = now.plusDays(1)) {
            Map<String,Object> map = new HashMap<>();
            map.put("begin", LocalDateTime.of(now, LocalTime.MIN));
            map.put("end", LocalDateTime.of(now, LocalTime.MAX));
            map.put("status",5);
            Double turnover = this.orderMapper.sumByMap(map);
            if (turnover != null) {
                turnoverJoiner.add(String.format("%.2f", turnover));
            }else{
                turnoverJoiner.add("0.00");
            }
            dateJoiner.add(now.toString());
        }
        return new TurnoverReportVO(dateJoiner.toString(),turnoverJoiner.toString());
    }
}
